<!DOCTYPE html><html lang="[&quot;zh-Hans&quot;,&quot;zh-CN&quot;,&quot;default&quot;]"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="rem和em的区别"><meta name="keywords" content="面试,css,CSS"><meta name="author" content="wenmu"><meta name="copyright" content="wenmu"><title>rem和em的区别 | 温木的博客</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.6.1"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.6.1"><link rel="dns-prefetch" href="https://cdn.staticfile.org"><link rel="dns-prefetch" href="https://cdn.bootcss.com"><link rel="dns-prefetch" href="https://creativecommons.org"><link rel="dns-prefetch" href="http://ta.qq.com"><script>(function() {
   var hm = document.createElement("script");
   hm.src = "https://tajs.qq.com/stats?sId=&lt;script type=&quot;text/javascript&quot; src=&quot;http://tajs.qq.com/stats?sId=66523541&quot; charset=&quot;UTF-8&quot;&gt;&lt;/script&gt;";
   var s = document.getElementsByTagName("script")[0];
   s.parentNode.insertBefore(hm, s);
 })();</script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  }
} </script><meta name="generator" content="Hexo 4.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#EM"><span class="toc-number">1.</span> <span class="toc-text">EM</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#REM"><span class="toc-number">2.</span> <span class="toc-text">REM</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://i.loli.net/2020/02/27/xwaBoOj158MfNyq.jpg"></div><div class="author-info__name text-center">wenmu</div><div class="author-info__description text-center">记录一些在工作中或学习新知识时学到的一些知识点；正所谓好记性不如一个烂笔头，温故而知新！</div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">115</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">52</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">30</span></a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">温木的博客</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus"><a class="site-page" href="/">首页</a><a class="site-page" href="/archives">文章</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a></span></div><div id="post-info"><div id="post-title">rem和em的区别</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-02-21</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/CSS/">CSS</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><h3 id="EM"><a href="#EM" class="headerlink" title="EM"></a>EM</h3><blockquote>
<ul>
<li>EM 是根据<code>自身字体</code>大小来转换成像素；</li>
<li>很多时候感觉是根据父节点来计算的，其实是子节点继承了父节点的字体大小，无论什么情况都是先计算出本身字体大小，然后再根据自身字体的大小把 em 转换成像素</li>
<li>如果本身的字体也是设置的 em，则先根据父节点的字体算出自身字体的大小</li>
</ul>
</blockquote>
<a id="more"></a>

<h3 id="REM"><a href="#REM" class="headerlink" title="REM"></a>REM</h3><blockquote>
<p>rem 是根据 html 标签的字体大小来转换成对应的像素，而 html 的字体大小又是相对于浏览器设置的字体大小，默认是 16px；<br>如果 html 的字体大小也设置成 rem，那么这个需要根据浏览器的字体进行转换。</p>
</blockquote>
<p>###demo</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang&#x3D;&quot;en&quot; style&#x3D;&quot;font-size:20px;&quot;&gt;</span><br><span class="line">    &lt;head&gt;</span><br><span class="line">        &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;</span><br><span class="line">        &lt;meta name&#x3D;&quot;viewport&quot; content&#x3D;&quot;width&#x3D;device-width, initial-scale&#x3D;1.0&quot;&gt;</span><br><span class="line">        &lt;meta http-equiv&#x3D;&quot;X-UA-Compatible&quot; content&#x3D;&quot;ie&#x3D;edge&quot;&gt;</span><br><span class="line">        &lt;title&gt;Document&lt;&#x2F;title&gt;</span><br><span class="line">        &lt;style&gt;</span><br><span class="line">            .divBorder &#123;</span><br><span class="line">                border: 1px solid black;</span><br><span class="line">                margin: 5px;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            .emContainer &#123;</span><br><span class="line">                font-size: 30px;</span><br><span class="line">                border: 1px solid black;</span><br><span class="line">                margin: 5px;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            .继承根节点 &#123;</span><br><span class="line">                margin-left: 0.8em;</span><br><span class="line">                border: 1px solid red;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            .继承 &#123;</span><br><span class="line">                margin-left: 0.8em;</span><br><span class="line">                border: 1px solid yellow;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            .本身设置字体 &#123;</span><br><span class="line">                font-size: 40px;</span><br><span class="line">                margin-left: 0.6em;</span><br><span class="line">                border: 1px solid green;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            .本身设置em字体 &#123;</span><br><span class="line">                font-size: 1.5em;</span><br><span class="line">                margin-left: 0.6em;</span><br><span class="line">                border: 1px solid green;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">             .rem继承根节点 &#123;</span><br><span class="line">                margin-left: 0.8rem;</span><br><span class="line">                border: 1px solid red;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            .rem继承 &#123;</span><br><span class="line">                margin-left: 0.8rem;</span><br><span class="line">                border: 1px solid yellow;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            .rem本身设置字体 &#123;</span><br><span class="line">                font-size: 40px;</span><br><span class="line">                margin-left: 0.6rem;</span><br><span class="line">                border: 1px solid green;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            .rem本身设置em字体 &#123;</span><br><span class="line">                font-size: 1.5rem;</span><br><span class="line">                margin-left: 0.6rem;</span><br><span class="line">                border: 1px solid green;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &lt;&#x2F;style&gt;</span><br><span class="line">    &lt;&#x2F;head&gt;</span><br><span class="line"></span><br><span class="line">    &lt;body&gt;</span><br><span class="line">        &lt;h2&gt;em是根据自身的字体大小来计算成对应的px值&lt;&#x2F;h2&gt;</span><br><span class="line"></span><br><span class="line">        &lt;div class&#x3D;&quot;divBorder&quot;&gt;</span><br><span class="line">            &lt;div class&#x3D;&quot;rem继承根节点&quot;&gt;测试继承根节点，em根据自身从父节点继承的字体大小来计算em的值。&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;emContainer&quot;&gt;</span><br><span class="line">            &lt;div class&#x3D;&quot;继承&quot;&gt;测试继承父节点的字体大小，em根据自身从父节点继承的字体大小来计算em的值。&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;emContainer&quot;&gt;</span><br><span class="line">            &lt;div class&#x3D;&quot;本身设置字体&quot;&gt;测试设置本身字体大小，em直接根据自身设置的字体大小进行计算。&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line"></span><br><span class="line">        &lt;div class&#x3D;&quot;emContainer&quot;&gt;</span><br><span class="line">            &lt;div class&#x3D;&quot;本身设置em字体&quot;&gt;本身字体以em方式设置，先计算本身字体大小，然后再根据自身字体大小计算其他的em设置&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line"></span><br><span class="line">        &lt;h2&gt;rem是根据html标签的字体大小来计算成对应的px值&lt;&#x2F;h2&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;divBorder&quot;&gt;</span><br><span class="line">            &lt;div class&#x3D;&quot;rem继承根节点&quot;&gt;测试继承根节点，em根据自身从父节点继承的字体大小来计算em的值。&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;emContainer&quot;&gt;</span><br><span class="line">            &lt;div class&#x3D;&quot;rem继承&quot;&gt;测试继承父节点的字体大小，em根据自身从父节点继承的字体大小来计算em的值。&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;emContainer&quot;&gt;</span><br><span class="line">            &lt;div class&#x3D;&quot;rem本身设置字体&quot;&gt;测试设置本身字体大小，em直接根据自身设置的字体大小进行计算。&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line"></span><br><span class="line">        &lt;div class&#x3D;&quot;emContainer&quot;&gt;</span><br><span class="line">            &lt;div class&#x3D;&quot;rem本身设置em字体&quot;&gt;本身字体以em方式设置，先计算本身字体大小，然后再根据自身字体大小计算其他的em设置&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;&#x2F;body&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>

<p><a href="http://caibaojian.com/rem-vs-em.html" target="_blank" rel="noopener">参考</a><br><a href="https://drafts.csswg.org/css-values-3/#absolute-lengths" target="_blank" rel="noopener">官网地址</a></p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">wenmu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.wangpengpeng.site/2020/02/21/rem%E5%92%8Cem%E7%9A%84%E5%8C%BA%E5%88%AB/">http://blog.wangpengpeng.site/2020/02/21/rem%E5%92%8Cem%E7%9A%84%E5%8C%BA%E5%88%AB/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noopener">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.wangpengpeng.site">温木的博客</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E9%9D%A2%E8%AF%95/">面试</a><a class="post-meta__tags" href="/tags/css/">css</a><a class="post-meta__tags" href="/tags/CSS/">CSS</a></div><div class="post-qr-code"><div class="post-qr-code-item"><img class="post-qr-code__img" src="https://i.loli.net/2020/02/27/hOBzIxFDJMCrmtN.jpg"><div class="post-qr-code__desc">微信打赏</div></div></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/02/25/%E5%93%8D%E5%BA%94%E5%BC%8F%E5%BC%80%E5%8F%91/"><i class="fa fa-chevron-left">  </i><span>响应式开发</span></a></div><div class="next-post pull-right"><a href="/2020/02/20/%E5%89%8D%E7%AB%AF%E7%9F%A5%E8%AF%86%E7%82%B9%E4%B8%B2%E8%AE%B2-3/"><span>前端知识点串讲-3</span><i class="fa fa-chevron-right"></i></a></div></nav><div id="vcomment"></div><script src="https://cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js"></script><script>var notify = 'false' == true ? true : false;
var verify = 'false' == true ? true : false;
var GUEST_INFO = ['nick','mail','link'];
var guest_info = 'nick,mail,link'.split(',').filter(function(item){
  return GUEST_INFO.indexOf(item) > -1
});
guest_info = guest_info.length == 0 ? GUEST_INFO :guest_info;
window.valine = new Valine({
  el:'#vcomment',
  notify:notify,
  verify:verify,
  appId:'U1QqpeQNuBnRsfygHk1GQ30q-gzGzoHsz',
  appKey:'dunDQzEseDzgsKgw4qLQcog1',
  placeholder:'支持markdown！',
  avatar:'wavatar',
  guest_info:guest_info,
  pageSize:'20',
  lang: 'zh-cn'
})</script></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2019 - 2020 By wenmu</div><div class="framework-info"><span>驱动 - </span><a href="http://hexo.io" target="_blank" rel="noopener"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a href="https://github.com/Molunerfinn/hexo-theme-melody" target="_blank" rel="noopener"><span>Melody</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv">该文访问量<span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.6.1"></script><script src="/js/fancybox.js?version=1.6.1"></script><script src="/js/sidebar.js?version=1.6.1"></script><script src="/js/copy.js?version=1.6.1"></script><script src="/js/fireworks.js?version=1.6.1"></script><script src="/js/transition.js?version=1.6.1"></script><script src="/js/scroll.js?version=1.6.1"></script><script src="/js/head.js?version=1.6.1"></script><script>if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
}</script></body></html>